#include "sResourceContent.h"
#include "sConfig.h"

#ifdef _EDITOR
#include <algorithm>
#endif

namespace sgui
{
namespace res
{

// -------------------------------------------- sResourceContent ---------------------------------------------

sResourceContent::sResourceContent(sGUI& gui) :
	sGUIObject(gui), m_recordType(L""), m_data(NULL), m_size(0)
{
    //
}
        
sResourceContent::~sResourceContent()
{
    clear();
}

const u32& sResourceContent::getDataSize() const
{
    return m_size;
}

void* sResourceContent::getData() const
{
    return m_data;
}

void sResourceContent::setDataSize(const u32& dataSize)
{
    m_size = dataSize;
}
    
void sResourceContent::clear()
{
    if (m_data)
    {
        free(m_data);
        m_data = NULL;
        m_size = 0;
    }
}

void sResourceContent::save(db::Record& file) const
{
	file[L"res_type"].Set(getRecordType());
	file[L"file_name"].Set(getFileName());
    db::Record& data = file[L"data"];
    data.Set(m_data, m_size);
}

void sResourceContent::load(const db::Record& file)
{
	setRecordType(file[L"res_type"].GetAsString());
	setFileName(file[L"file_name"].GetAsString());

    const db::Record& data = file[L"data"];

    m_size = data.GetDataSize();
    if (m_size)
    {
        m_data = malloc(m_size);
        memcpy(m_data, data.GetAsData(), m_size);
    }
}

#ifdef _EDITOR
void sResourceContent::loadFromFile(const std::wstring& fileName)
{
	clear();
	
	FILE* f; 
	_wfopen_s(&f, fileName.c_str(), L"rb");
	if (f)
	{			
		fseek(f, 0L, SEEK_END);
		m_size = ftell(f);
		rewind(f);
		m_data = malloc(m_size);
		fread(m_data, 1, m_size, f);
				
		s32 dotPos = fileName.find(L".");
		if (dotPos != std::wstring::npos)
		{
			setRecordType(fileName.substr(dotPos + 1));
		}
		else
		{
			setRecordType(L"");
		}

		std::wstring path = fileName;
		std::replace(path.begin(), path.end(), '\\', '/');
		dotPos = path.find_last_of(L"/");
		if (dotPos != std::wstring::npos)
		{
			setFileName(path.substr(dotPos + 1));
		}
		else
		{
			setFileName(path);
		}

		fclose(f);
	}
}
	
void sResourceContent::saveToFile(const std::wstring& fileName)
{
	FILE* f; 
	_wfopen_s(&f, fileName.c_str(), L"wb");
	if (f)
	{		
		fwrite(m_data, m_size, 1, f);
		fclose(f);
	}
}

void sResourceContent::exportRTTI(rtti::sInfoList& list)
{
	REGISTER_RTTI(L"File name", TEXT, NEW_RTTI_PARAMETER(
		STRING, sResourceContent, this, &sResourceContent::setFileName, &sResourceContent::getFileName
	));
	REGISTER_RTTI(L"Resource type", TEXT, NEW_RTTI_PARAMETER(
		STRING, sResourceContent, this, &sResourceContent::setRecordType, &sResourceContent::getRecordType
	));
}
#endif

const std::wstring& sResourceContent::getRecordType() const
{
	return m_recordType;
}

void sResourceContent::setRecordType(const std::wstring& recordType)
{
	m_recordType = recordType;
}

const std::wstring& sResourceContent::getFileName() const
{
	return m_fileName;
}

void sResourceContent::setFileName(const std::wstring& fileName)
{
	m_fileName = fileName;
}
	
std::wstring sResourceContent::getExtention() const
{
	int pos = m_fileName.find('.');
	if (pos != std::wstring::npos)
	{
		return m_fileName.substr(pos + 1);
	}
	return m_fileName;
}

};
};